/*
 * @Descripttion: 
 * @Author: xgh
 * @Date: 2022-09-08 15:19:25
 * @LastEditors: xgh
 * @LastEditTime: 2022-09-19 16:01:53
 */

var express = require('express');
var query = require('../connent/mysqlPool') // 引入数据库连接池文件
let format = require('./format');
var router = express.Router();



let handleTimeSort = (data) => {
  console.log(data);
  let logInfo = data.map((item, index) => {
    item.handle_time = new Date(item.handle_time).getTime()
    return item
  })
  logInfo.sort((x, y) => {
    return y.handle_time - x.handle_time
  })

  let result = logInfo.map(item => {
    item.handle_time = new Date(item.handle_time).format("yyyy-MM-dd hh:mm:ss");
    return item
  })
  return result
}

/* 获取当前日志信息 */
router.post('/getlog', (req, res) => {
  let searchParams = req.body
  let startTime = new Date(searchParams.time[0]).format("yyyy-MM-dd hh:mm:ss")
  let endTime = new Date(searchParams.time[1]).format("yyyy-MM-dd hh:mm:ss")
  query("select * from user_log_info where handle_time >= ? and handle_time <= ?", [startTime,endTime], (err, result, fields) => {
    res.send({
      code: 1,
      data: handleTimeSort(result)
    })
  })
})


/* 添加用户日志 */
router.post('/adduserlog', (req, res) => {
  let id = req.body.id ||0           //判断是否登陆成功
  let username = req.body.username
  let function_module = req.body.function_module
  let search_condation = req.body.search_condation
  let handle = req.body.handle
  userInfoSql = `select * from user where username=?`
  addUserlogSql = "insert into user_log_info(user_id,username,ip,department,function_module,search_condation,handle,handle_status,handle_time) values(?,?,?,?,?,?,?,?,?)"
  query(userInfoSql,[username], (err, result, fields) => {    //查询用户信息
    let userInfo=result[0]
    let handletatus=""
    if(id===0){  //操作失败
      handletatus =0
    }else{
      handletatus =1
    }
    let parmas = [userInfo.id, userInfo.username, userInfo.ip, userInfo.department, function_module, search_condation, handle, handletatus, new Date()]
    query(addUserlogSql, parmas, (err, result, fields) => {
      res.send({
        message:1
      })
    })
  })
})

/* 日志搜索 */
router.post('/searchlog', (req, res) => {
  let searchParams = req.body
  let startTime = new Date(searchParams.time[0]).format("yyyy-MM-dd hh:mm:ss")
  let endTime = new Date(searchParams.time[1]).format("yyyy-MM-dd hh:mm:ss")
  let type = searchParams.type
  let searchword = searchParams.searchword
  let searchLogSqlq
  let searchCondation
  switch (type) {
    case "不限":
      searchLogSqlq = `select * from user_log_info where handle_time >= ? and handle_time <= ?`
      searchCondation = [startTime, endTime]
      break;
    case "姓名":
      type ="username"
      break;
    case "所属部门":
      type ="department"
      break;
    case "功能模块":
      type ="function_module"
      break;
    case "搜索条件":
      type ="search_condation"
      break;
    case "ip":
      type ="ip"
      break;
    default:
      break;
  }
  if(type!=="不限"){
    searchLogSqlq = `select * from user_log_info where handle_time >= ? and handle_time <= ? and ${type}=?`
    searchCondation = [startTime, endTime, searchword]
  }
  query(searchLogSqlq, searchCondation, (err, result, fields) => {
    console.log("---",result);
    res.send({
      code: 1,
      data: handleTimeSort(result)
    })
  })
})

module.exports = router
